﻿@page "/JavaMapper"
@using JavaDevToys.Rcl.Models
@using JavaDevToys.Rcl.Service

<MAlert Dismissible
        Type="AlertTypes.Error"
        @bind-Value="_alert">
    @errMsg
</MAlert>
<h3>Mapper类生成</h3>
<MContainer Fluid>
    <MRow Align="@AlignTypes.Center">
        <MCol Cols="6">
            <div class="d-flex pt-6">
                <MSelect Items="settingInfo.JavaProjectInfoList"
                         @bind-Value="@selectedJavaProjectName"
                         ItemText="u => u.Desc"
                         ItemValue="u => u.Name"
                         Filled
                         Label="选择工程"
                         FullWidth="false"
                         Dense></MSelect>
                <MTextField @bind-Value="@tableName" FullWidth="false"
                            Hint="This field uses counter prop"
                            Label="表名">
                </MTextField>
                <MButton Color="primary" Class="mt-2  ml-9" OnClick="CreateJavaMode">生成类</MButton>
            </div>
        </MCol>
    </MRow>
    <MRow>
        <MSelect Items="domainPathList"
                 @bind-Value="@selectedDomainPath"
                 ItemText="u => u"
                 ItemValue="u => u"
                 Filled
                 Label="Domain输出路径"
                 FullWidth="false"
                 Dense></MSelect>
    </MRow>
    <MRow>
        <MSelect Items="mapperPathList"
                 @bind-Value="@selectedMapperPath"
                 ItemText="u => u"
                 ItemValue="u => u"
                 Filled
                 Label="Mapper输出路径"
                 FullWidth="false"
                 Dense></MSelect>
    </MRow>
    <MRow>
        <MSelect Items="daoPathList"
                 @bind-Value="@selectedDaoPath"
                 ItemText="u => u"
                 ItemValue="u => u"
                 Filled
                 Label="Dao输出路径"
                 FullWidth="false"
                 Dense></MSelect>
    </MRow>
    <MRow>
        <MSelect Items="daoImplPathList"
                 @bind-Value="@selectedDaoImplPath"
                 ItemText="u => u"
                 ItemValue="u => u"
                 Filled
                 Label="DaoImpl输出路径"
                 FullWidth="false"
                 Dense></MSelect>
    </MRow>
    <MRow Align="@AlignTypes.Center">
        <MList>
            @if (outFilePathList != null)
            {
                <MButton Color="primary" Class="mt-2  ml-9" OnClick="AddGit">添加到git</MButton>
                @foreach (var item in outFilePathList)
                {
                    <MListItem>
                        <MListItemAction>
                            <MIcon>@item.Value</MIcon>
                        </MListItemAction>

                        <MListItemContent>
                            <MListItemTitle>@item.Key</MListItemTitle>
                        </MListItemContent>
                    </MListItem>
                }
            }
        </MList>
    </MRow>
</MContainer>
@code {
    private bool _alert = false;
    private string errMsg = string.Empty;
    private string _selectedJavaProjectName;
    public string selectedJavaProjectName
    {
        get => _selectedJavaProjectName;
        set
        {
            if (value != _selectedJavaProjectName)
            {
                _selectedJavaProjectName = value;
                OnJavaProjectSelectedChanged(value);
            }
        }
    }
    private string tableName;
    private SettingInfo settingInfo;
    [Inject]
    public IJavaMapperParser javaMapperParser { get; set; }
    [Inject]
    public IJavaCodeGenerator javaCodeGenerator { get; set; }
    [Inject]
    public IApiInfoParser apiInfoParser { get; set; }
    [Inject]
    public IJavaProjectService javaProjectService { get; set; }
    [Inject]
    public ISettingService settingService { get; set; }
    [Inject]
    public IDirectoryPicker directoryPicker { get; set; }
    private Dictionary<string, string> outFilePathList;

    private List<string> domainPathList = new List<string>();
    private List<string> mapperPathList = new List<string>();
    private List<string> daoPathList = new List<string>();
    private List<string> daoImplPathList = new List<string>();
    private string selectedDomainPath;
    private string selectedMapperPath;
    private string selectedDaoPath;
    private string selectedDaoImplPath;

    protected override void OnInitialized()
    {
        base.OnInitialized();
        settingInfo = settingService.GetSettings(0);
    }

    private void OnJavaProjectSelectedChanged(string value)
    {
        JavaProjectInfo javaProjectInfo = settingInfo.JavaProjectInfoList.FirstOrDefault(f => f.Name == selectedJavaProjectName);
        if (string.IsNullOrWhiteSpace(javaProjectInfo.FullPath))
        {
            errMsg = "项目路径未设置，请点击右上角设置按钮进行设置";
            _alert = true;
            return;
        }
        if (string.IsNullOrWhiteSpace(javaProjectInfo.sqlserverInfo.Database))
        {
            errMsg = "数据库连接未设置，请点击右上角设置按钮进行设置";
            _alert = true;
            return;
        }
        mapperPathList = directoryPicker.SearchAllDirectory(javaProjectInfo.FullPath, "mapper");
        domainPathList = directoryPicker.SearchAllDirectory(javaProjectInfo.FullPath, "domain");
        daoPathList = directoryPicker.SearchAllDirectory(javaProjectInfo.FullPath, "dao");
        daoImplPathList = daoPathList.Select(s => Path.Combine(s, "impl")).ToList();

        selectedMapperPath = mapperPathList.FirstOrDefault();
        selectedDomainPath = domainPathList.FirstOrDefault();
        selectedDaoPath = daoPathList.FirstOrDefault();
        selectedDaoImplPath = daoImplPathList.FirstOrDefault();
    }

    private void CreateJavaMode()
    {
        JavaProjectInfo javaProjectInfo = settingInfo.JavaProjectInfoList.FirstOrDefault(f => f.Name == selectedJavaProjectName);
        if (javaProjectInfo == null)
        {
            errMsg = "请选择工程项目";
            _alert = true;
            return;
        }
        if (string.IsNullOrWhiteSpace(javaProjectInfo.FullPath))
        {
            errMsg = "项目路径未设置，请点击右上角设置按钮进行设置";
            _alert = true;
            return;
        }
        if (string.IsNullOrWhiteSpace(tableName))
        {
            errMsg = "表名不能为空";
            _alert = true;
            return;
        }
        if (string.IsNullOrWhiteSpace(javaProjectInfo.sqlserverInfo.Database))
        {
            errMsg = "数据库连接未设置，请点击右上角设置按钮进行设置";
            _alert = true;
            return;
        }
        if (string.IsNullOrWhiteSpace(selectedMapperPath) || string.IsNullOrWhiteSpace(selectedDomainPath) || string.IsNullOrWhiteSpace(selectedDaoPath) || string.IsNullOrWhiteSpace(selectedDaoImplPath))
        {
            errMsg = "未选择输出路径，请先选择输出路径，如果没有输出路径请先在对应项目建立相应路径!";
            _alert = true;
            return;
        }
        javaProjectInfo.MapperPath = selectedMapperPath;
        javaProjectInfo.DomainPath = selectedDomainPath;
        javaProjectInfo.DaoPath = selectedDaoPath;
        javaProjectInfo.DaoImplPath = selectedDaoImplPath;
        // 解析内容
        var javaMapper = javaMapperParser.Parse(tableName);
        // 生成Java类
        var pathList = javaCodeGenerator.GenerateMapper(javaProjectInfo, javaMapper);
        outFilePathList = new Dictionary<string, string>();
        foreach (var item in pathList)
        {
            outFilePathList.Add(item, "mdi-label");
        }
    }

    private void AddGit()
    {
        foreach (var item in outFilePathList)
        {
            if (javaProjectService.AddGitRepository(item.Key, out string errMsg))
            {
                outFilePathList[item.Key] = "mdi-check";
            }
        }
    }
}
